/*
 * BA_Console.java
 */
package behavior_analysis_ui;

import java.util.ArrayList;
import java.util.List;

import behavior_analysis_control.DialogErrorException;
import behavior_analysis_control.EndProgramException;
import behavior_analysis_control.FileUtils;
import behavior_analysis_control.POIFinder;
import behavior_analysis_control.POINotFoundException;
import behavior_analysis_control.TrackUtils;
import behavior_analysis_model.Analyzer;
import behavior_analysis_model.Track;

/**
 * Console version of the Behavior Analysis program.
 * 
 * @author Sean Brown
 * @version 11/1/2012
 */
public class BA_Console {
	
	/**
	 * Track list.
	 */
	private final List<Track> my_tracks;
	
	/**
	 * Constructor instantiates the Track list.
	 */
	public BA_Console() {
		my_tracks = new ArrayList<Track>();
	}
	
	/**
	 * The main method, invokes the Behavior Analysis program. Command line arguments are
	 * ignored.
	 * 
	 * @param the_args
	 *            Command line arguments.
	 */
	public static void main(final String... the_args) {
		BA_Console ba = new BA_Console();
		while(true) {
			try {
				ba.start();
			} catch (final DialogErrorException the_e) {
				System.err.println(the_e.getMessage());
			} catch (final EndProgramException the_e) {
				System.out.println(the_e.getMessage());
				break;
			}
		}
		System.exit(0);
	}

	/**
	 * Start method begins automation to prompt user for file name,
	 * process request, and display results.
	 * 
	 * @throws EndProgramException When the user wants to exit.
	 * @throws DialogErrorException When the program generates a non-fatal error.
	 */
	public void start() throws EndProgramException, DialogErrorException {
		welcomeMessage();
		
		//Get file name from user.
		final String file_name = FileUtils.readFromConsole();
		
		//Validate csv file.
		FileUtils.validateFile(file_name); //throws DEE back to main
		
		final String extention = FileUtils.getExtension(file_name); //throws DEE back to main
		
		if (extention.equals("csv")) {
			//Read file to Track objects
			try {
				TrackUtils.CSVToTracks(file_name, my_tracks, null);
			} catch (final DialogErrorException e) {
				System.err.println(e.getMessage());
			}
	    } else if (extention.equals("gpx")) {
	    		//TrackUtils.GPXToTracks(file_name, my_tracks));
	    } else { //throw back to main
	    	throw new DialogErrorException("You have chosen a ." + extention + " file."
	    			+ " Please choose a valid file.");
	    }
		
		//TODO: Use database for analyzing
		//Parse csv file and add to tables.
		//final SQLCreator sc = new SQLCreator();
		//sc.populateTables(entries);
		
		//Analyze the gps data.
		final Analyzer analyzer = new Analyzer();
		analyzer.analyzeTracks(my_tracks, null);
		
		final ArrayList<Track> pois = new ArrayList<Track>();
		pois.addAll(analyzer.getPOIs().keySet());
		final POIFinder pf = new POIFinder(pois);
		
		displayResults(pf);

		exitMessage();
	}

	/**
	 * Prints welcome message to the screen.
	 */
	private void welcomeMessage() {
		System.out.println("Please type the path of the file that contains GPS traces...");
	}
	
	private void displayResults(final POIFinder the_pf) {
		System.out.println("Home: ");
		try {
			System.out.println("\t" + TrackUtils.getAddress(the_pf.getHome()));
		} catch (final POINotFoundException p) {
			System.err.println(p.getMessage());
		}
		try {
			System.out.println("\t" + the_pf.getHome().latLongToString());
			System.out.println("\t" + the_pf.getHome().totalTimeToString());
			System.out.println();
		} catch (final POINotFoundException p) {
			System.err.println(p.getMessage());
		}
		
		System.out.println("Work: ");
		try {
			System.out.println("\t" + TrackUtils.getBusinessName(the_pf.getWork()));
		} catch (final POINotFoundException p) {
			System.err.println(p.getMessage());
		}
		try {
			System.out.println("\t" + TrackUtils.getAddress(the_pf.getWork()));
		} catch (final POINotFoundException p) {
			System.err.println(p.getMessage());
		}
		try {
			System.out.println("\t" + the_pf.getWork().latLongToString());
			System.out.println("\t" + the_pf.getWork().totalTimeToString());
			System.out.println();
		} catch (final POINotFoundException p) {
			System.err.println(p.getMessage());
		}
		
		
		for (int i = 0; i < POIFinder.NUM_POIS; i++) {
			try {
				System.out.println("POI #" + (i + 1) + ": ");
				System.out.println("\t" + TrackUtils.getBusinessName(the_pf.getPOI(i)));
			} catch (final POINotFoundException p) {
				//don't print
			}
			try {
				System.out.println("\t" + TrackUtils.getAddress(the_pf.getPOI(i)));
			} catch (final POINotFoundException p) {
				System.err.println(p.getMessage());
			}
			try {
				System.out.println("\t" + the_pf.getPOI(i).latLongToString());
				System.out.println("\t" + the_pf.getPOI(i).totalTimeToString());
				System.out.println();
			} catch (final POINotFoundException p) {
				System.err.println(p.getMessage());
			}
		}
		/* Prints all results
		for (Track key : the_pois.keySet()) {
			System.out.println(key.toStringAlt());
			final ArrayList<TimedTrack> temp = the_pois.get(key);
			for (TimedTrack t : temp) {
				System.out.print("\t");
				System.out.println(t);
			}
		} */
	}
	
	/**
	 * Prints exit question to the screen, and throws exception if the user
	 * wants to exit.
	 * 
	 * @throws EndProgramException Thrown if user wishes to quit.
	 */
	private void exitMessage() throws EndProgramException {
		System.out.println("Would you like to analyze another file? (Y or N)");
		if (FileUtils.readFromConsole().trim().equalsIgnoreCase("Y")) {
			//do nothing, start over
		} else if (FileUtils.readFromConsole().trim().equalsIgnoreCase("N")) {
			throw new EndProgramException("Thanks for using this software.  Goodbye!");
		} else {
			System.out.println("Invalid entry.");
			//try again
			exitMessage();
		}
	}

}
